package Grantsfire::Controller::Org;

use strict;
use warnings;
use base 'Catalyst::Controller';
use URI::QueryParam;

sub grantee : Regex('^org/grantee$') {
    _setup_org(@_);
}

sub grantor : Regex('^org/grantor$') {
    _setup_org(@_);
}

sub _setup_org {
    my ($self, $c, $id, $type) = @_;
    my $vcard = $c->model("GF::Vcard")->find($id);
    my ($org) = $c->model("GF::Org")->search(
        {
            'vcard.url' => $vcard->url,
        },
        {
            join => [qw/vcard/],
        }
    );

    # add search results for grants related to this grantor/ee
    if ($vcard) {
        $c->req->params->{q} = $vcard->org;
        ($c->req->params->{by} = $c->pageid) =~ s/^org_//;
        $c->req->uri->query_param($_, $c->req->params->{$_}) for qw/by q/;
        $c->forward('/grant/search', [rows => 5]);
    }
    $c->stash->{vcard} = $vcard;
    $c->stash->{org} = $org;
}

1;
